#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>> ret;
        int left = 0;
        while (left < n - 2)
        {
            int right = n - 1;
            int mid = left + 1;
            while (mid < right)
            {
                if (nums[mid] + nums[left] + nums[right] > 0)
                {
                    right--;
                }
                else if (nums[mid] + nums[left] + nums[right] < 0)
                {
                    mid++;
                }
                else
                {
                    vector<int> v;
                    v.push_back(nums[left]);
                    v.push_back(nums[mid]);
                    v.push_back(nums[right]);
                    ret.push_back(v);
                    while (mid < right && (nums[mid] == nums[mid + 1]))
                    {
                        mid++;
                    }
                    mid++;
                }
            }
            left++;
            while (left < n - 2 && (nums[left - 1] == nums[left]))
            {
                left++;
            }
        }
        return ret;
    }
};

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int n = nums.size();
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        int i = 0;
        while (i < n - 3)
        {
            long long sum = target - nums[i];
            int left = i + 1;
            while (left < n - 2)
            {
                int right = n - 1;
                int mid = left + 1;
                while (mid < right)
                {
                    if ((long long)nums[left] + nums[mid] + nums[right] < sum)
                    {
                        mid++;
                    }
                    else if ((long long)nums[left] + nums[mid] + nums[right] > sum)
                    {
                        right--;
                    }
                    else
                    {
                        vector<int> v;
                        v.push_back(nums[i]);
                        v.push_back(nums[left]);
                        v.push_back(nums[mid]);
                        v.push_back(nums[right]);
                        ret.push_back(v);
                        while (mid < right && nums[mid] == nums[mid + 1])
                        {
                            mid++;
                        }
                        mid++;
                    }
                }
                left++;
                while (left < n - 2 && nums[left] == nums[left - 1])
                {
                    left++;
                }
            }
            i++;
            while (i < n - 3 && nums[i - 1] == nums[i])
            {
                i++;
            }
        }
        return ret;
    }
};
